package controller

import (
	"gin/response"
	"gin/service"
	"gin/types"
	"github.com/gin-gonic/gin"
)

var (
	taskRepairService service.TaskRepair
)

type TaskRepair struct {
}

// GetWaitAssignStepCategoryCount 任务待分配分类统计
func (c *TaskRepair) GetEffectedTask(ctx *gin.Context) {
	var req types.GetEffectedTaskReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	tenantId := service.GetInt64FromContext(ctx, "tenant_id")

	resData, queryErr := taskRepairService.GetEffectedTask(req, tenantId)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// GetUpstreamTasks 查询任务上游任务列表
func (c *TaskRepair) GetUpstreamTasks(ctx *gin.Context) {
	var req types.GetUpstreamTasksReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	resData, queryErr := taskRepairService.GetUpstreamTasks(req.TaskId)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

//GetMultiProjectEffectedGroupTask  查询受影响多项目的分组任务
func (c *TaskRepair) GetMultiProjectEffectedGroupTask(ctx *gin.Context) {
	var req types.GetMultiProjectEffectedGroupTaskReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	tenantId := service.GetInt64FromContext(ctx, "tenant_id")

	resData, queryErr := taskRepairService.GetMultiProjectEffectedGroupTask(req, tenantId)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}
